package dao;

import pojo.entity.Goods;
import pojo.entity.ShoppingCart;
import pojo.entity.User;
import utils.DbUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public interface ShoppingCartDao {
    //为购物车添加商品
    public static void addShoppingCart(ShoppingCart shoppingCart) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql
        String sql = "INSERT INTO shopping_cart(user_id, goods_id, goods_name, price, quantity)"
                + "values(" + "?,?,?,?,?)";
        //预编译
        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        //传参
        ptmt.setInt(1, shoppingCart.getUserId());
        ptmt.setInt(2, shoppingCart.getGoodsId());
        ptmt.setString(3, shoppingCart.getGoodsName());
        ptmt.setDouble(4, shoppingCart.getPrice());
        ptmt.setInt(5, shoppingCart.getQuantity());

        //执行
        ptmt.execute();
    }
    //根据id修改购物车商品数量
    public static void updateSCQuantityById(ShoppingCart shoppingCart,Integer count) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "UPDATE shopping_cart" +
                " set quantity=?"+
                " where id=?";
        //预编译
        PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL，减少sql执行

        //传参
        ptmt.setInt(1, count);
        ptmt.setInt(2, shoppingCart.getId());

        //执行
        ptmt.execute();
    }


//    //根据id删除购物车商品
//    public static void delSCGoodsById(ShoppingCart shoppingCart) throws SQLException {
//        //获取连接
//        Connection conn = DbUtil.getConnection();
//        //sql, 每行加空格
//        String sql = "delete from shopping_cart where id=?";
//        //预编译SQL，减少sql执行
//        PreparedStatement ptmt = conn.prepareStatement(sql);
//
//        //传参
//        ptmt.setInt(1, shoppingCart.getId());
//
//        //执行
//        ptmt.execute();
//    }
    //根据id批量删除购物车商品
    public static void delBatchSCGoodsById(int[] ids) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        for (int id : ids) {
            //sql, 每行加空格
            String sql = "delete from shopping_cart where id=?";
            //预编译SQL，减少sql执行
            PreparedStatement ptmt = conn.prepareStatement(sql);

            //传参
            ptmt.setInt(1, id);
            //执行
            ptmt.execute();
        }
    }
    //根据用户id清空购物车
    public static void delAllSCGoods(Integer userId) throws SQLException {
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "delete from shopping_cart where user_id= ?";
        //预编译SQL，减少sql执行
        PreparedStatement ptmt = conn.prepareStatement(sql);

        //传参
        ptmt.setInt(1, userId);
        //执行
        ptmt.execute();
        }

     //根据用户id查询购物车商品
    public static List<ShoppingCart> querySCGoodsByUserId(Integer userId) throws SQLException {
        Connection conn = DbUtil.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "SELECT * FROM shopping_cart WHERE user_id = ? order by id ASC,price desc";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, userId);  // 传参
        ResultSet rs = pstmt.executeQuery();
        List<ShoppingCart> sc = new ArrayList<>();
        ShoppingCart cart = null;
        while(rs.next()){
            cart = new ShoppingCart();
            cart.setId(rs.getInt("id"));
            cart.setUserId(rs.getInt("user_id"));
            cart.setGoodsId(rs.getInt("goods_id"));
            cart.setGoodsName(rs.getString("goods_name"));
            cart.setPrice(rs.getDouble("price"));
            cart.setQuantity(rs.getInt("quantity"));

            sc.add(cart);
        }
        return sc;
    }

    //根据id查询购物车中商品
    public static ShoppingCart getSCById(Integer id) throws SQLException {
        ShoppingCart goods = null;
        //获取连接
        Connection conn = DbUtil.getConnection();
        //sql, 每行加空格
        String sql = "select * from  shopping_cart where id=?";
        //预编译SQL，减少sql执行
        PreparedStatement ptmt = conn.prepareStatement(sql);
        //传参
        ptmt.setInt(1, id);
        //执行
        ResultSet rs = ptmt.executeQuery();
        while(rs.next()){
            goods = new ShoppingCart();
            goods.setId(rs.getInt("id"));
            goods.setUserId(rs.getInt("user_id"));
            goods.setGoodsId(rs.getInt("goods_id"));
            goods.setGoodsName(rs.getString("goods_name"));
            goods.setPrice(rs.getDouble("price"));
            goods.setQuantity(rs.getInt("quantity"));
        }
        return goods;
    }

}
